<template>
    <div class="trigger-condition">
        <a class="btn btn-primary btn-mini" href="javascript:;" @click="add">添加新行为</a>
        <table class="table table-striped ">
            <tr>
                <th width="100">序号</th>
                <th width="200">类型</th>
                <th>内容</th>
                <th>操作</th>
            </tr>
            <tbody>
            <tr v-for="(v,i) in data.action">
                <td valign="center">
                   [{{i+1}}]
                </td>
                <td>
                    <select v-model="v.type" class="form-control">
                        <option value="ACTION_SEND_NOTICE">发送消息</option>
                        <option value="ACTION_START_BPM">启动流程</option>
                    </select>
                </td>
                <td>
                    <div v-if="v.type == 'ACTION_SEND_NOTICE'">
                        给
                        <select v-model="v.args.to">
                            <option value="PUBLISHER">流程发起人</option>
                            <option value="CURRENT_DEALERS">当前节点经办人</option>
                            <option value="ALL_DEALERS">所有节点经办人</option>
                            <option value="CAN_COMMENT">拥有批注权限的人</option>
                        </select>
                        发送系统通知，内容为
                        <br>
                        <textarea v-model="v.args.content" class="form-control"></textarea>
                    </div>
                    <div v-if="v.type == 'ACTION_START_BPM'">
                        给
                        <select v-model="v.args.to">
                            <option value="PUBLISHER">流程发起人</option>
                            <option value="CURRENT_DEALERS">当前节点经办人</option>
                            <option value="ALL_DEALERS">所有节点经办人</option>
                        </select>
                        启动新流程（如果没有执行权限则启动失败）
                        <select v-model="v.args.bpmId">
                            <optgroup v-for="v in modelsWithCat" :label="v.name">
                                <option v-for="vv in v.children" :value="vv._id">{{vv.modelName}}</option>
                            </optgroup>
                        </select>
                    </div>
                </td>
                <td>
                    <a href="javascript:;" class="btn btn-danger btn-mini" @click="remove(i)">删除</a>
                </td>
            </tr>
            </tbody>
        </table>
    </div>
</template>
<script>
    var component = {
        props:["data"],
        data(){
            return {
                modelsWithCat: [],
                models: {},
            }
        },
        methods:{
            add(){
                this.data.action.push({
                    type:"ACTION_SEND_NOTICE",
                    args: {
                        to: "PUBLISHER",
                        bpmId: "",
                        custom: [],
                        content: ""
                    }
                })
            },
            remove(i){
                this.data.action.splice(i,1);
            }
        },
        created(){
            var t = this;
            $.get("/api/bpm/workflow/getWorkflowNames2", function (msg) {
                if(!msg.success){
                    return;
                }
                Vue.set(t, "modelsWithCat", msg.data);
                // console.log(t.modelsWitchCat)
                (msg.data||[]).forEach(ee => {
                    ee.children.forEach(e => {
                        Vue.set(t.models, e._id, e.modelName)
                    })
                });
            },"json");
        }
    }
</script>